<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\ResponseException;
use App\Models\CouponRelationGoods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CouponsController extends Controller
{
    /**
     * 我的优惠券列表
     * User: Clannad ~ ☆
     * DataTime：2020/5/7 0007 16:27
     * @param Request $request
     * @throws ResponseException
     */
    public function index(Request $request)
    {
        $status = $request->input('status');

        $where = [
            'status'=>$status
        ];

        $couponList = auth('api')->user()->coupons()
                    ->where($where)
                    ->with([
                        'coupon'=>function($query){ //预加载的是coupons模型下的关联
                            return $query->select('id','coupon_name');
                        }
                    ])->paginate(10);

        //优惠券类型 1.店铺满减 2.店铺折扣 3.通用满减 4.通用折扣 5.免邮 6.指定商品
        foreach ($couponList as $k => $v) {
            if($v['coupon_type'] == 1 || $v['coupon_type'] == 2){ //店铺

            }else if($v['coupon_type'] == 6){
                $goods_name = CouponRelationGoods::query()
                                ->select('goods.goods_name','coupon_relation_goods.numbers')
                                ->where([
                                        'coupon_relation_goods.coupon_id'=>$v['coupon_id'],
                                    ])
                                ->leftJoin('goods','coupon_relation_goods.goods_id','=','goods.id')
                                ->first();

                $couponList[$k]['goods_name'] = $goods_name['goods_name'];
                $couponList[$k]['goods_number'] = $goods_name['numbers'];
            }
        }

        throw new ResponseException(200,'ok',$couponList);
    }
}
